package com.tpo.jnotes.client.layer.view.dialogs;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JTextPane;
import javax.swing.border.MatteBorder;

import com.tpo.jnotes.base.colors.schema.BCSSManager;
import com.tpo.jnotes.base.resource.BResConstants;
import com.tpo.jnotes.base.resource.BResource;
import com.tpo.jnotes.client.layer.binding.BindingsEnum;
import com.tpo.jnotes.client.layer.controller.commands.AddCommand;
import com.tpo.jnotes.client.layer.controller.commands.DeleteCommand;
import com.tpo.jnotes.client.layer.view.components.NDialogButton;
import com.tpo.jnotes.client.layer.view.components.NTitleEditor;
import com.tpo.jnotes.client.layer.view.dialogs.listeners.NEditorListener;
import com.tpo.jnotes.client.layer.view.dialogs.listeners.NFrameFocusListener;
import com.tpo.jnotes.client.layer.view.dialogs.listeners.NTitleMouseListener;
import com.tpo.jnotes.client.lib.interfaces.IViewEntity;

public abstract class NBaseFrame extends JFrame implements IViewEntity, NIFrame
{

    protected NDialogButton addButton;
    protected NDialogButton deleteButton;

    protected JTextPane noteEditor;
    protected NTitleEditor titleEditor;
    protected JMenuBar menuBar;

    public NBaseFrame()
    {
        setSize( new Dimension( 200, 160 ) );
        setResizable( false );
        setUndecorated( true );
        setBackground( BCSSManager.getCurrentCSS().getPanelBackground() );
        getContentPane().setLayout( new BoxLayout( getContentPane(), BoxLayout.X_AXIS ) );
        setIconImage( BResource.getImage( BResConstants.MAIN_IMG ) );

        noteEditor = new JTextPane();
        noteEditor.setMargin( new Insets( 0, 0, 0, 0 ) );
        noteEditor.setBorder( new MatteBorder( 1, 1, 1, 1, BCSSManager.getCurrentCSS().getBorderColor() ) );
        noteEditor.setSelectionColor( BCSSManager.getCurrentCSS().getPanelForeground() );
        noteEditor.setFont( BCSSManager.getCurrentCSS().getPanelFont() );
        noteEditor.setBackground( BCSSManager.getCurrentCSS().getPanelBackground() );
        noteEditor.setForeground( BCSSManager.getCurrentCSS().getPanelForeground() );
        getContentPane().add( noteEditor );

        menuBar = new JMenuBar();
        menuBar.setBorder( null );
        menuBar.setAlignmentY( Component.CENTER_ALIGNMENT );
        // menuBar.setOpaque( false );
        menuBar.setBackground( BCSSManager.getCurrentCSS().getTitleBackground() );
        setJMenuBar( menuBar );

        addButton = new NDialogButton( BResource.getIcon( BResConstants.ADD_IMG ), AddCommand.ADD );
        menuBar.add( addButton );

        Component HorizontalGlueLeft = Box.createHorizontalGlue();
        menuBar.add( HorizontalGlueLeft );

        titleEditor = new NTitleEditor( this );
        titleEditor.setOpaque( false );
        menuBar.add( titleEditor );

        Component HorizontalGlueRight = Box.createHorizontalGlue();
        menuBar.add( HorizontalGlueRight );

        deleteButton =
            new NDialogButton( BResource.getIcon( BResConstants.DELETE_IMG ), DeleteCommand.DELETE );
        menuBar.add( deleteButton );
        addListeners();
    }

    private void addListeners()
    {
        this.addWindowFocusListener( new NFrameFocusListener( this ) );

        NTitleMouseListener titleListener = new NTitleMouseListener( this );
        menuBar.addMouseListener( titleListener );
        menuBar.addMouseMotionListener( titleListener );
        titleEditor.addMouseListener( titleListener );
        titleEditor.addMouseMotionListener( titleListener );

        noteEditor.addMouseListener( new NEditorListener( this ) );
    }

    @Override
    public Component getComponent()
    {
        return this;
    }

    @Override
    public void setActionListener( ActionListener aListener )
    {
        addButton.addActionListener( aListener );
        deleteButton.addActionListener( aListener );
    }

    public String getNoteText()
    {
        return noteEditor.getText();
    }

    public String getNoteTitle()
    {
        return titleEditor.getText();
    }

    public void save()
    {
        firePropertyChange( BindingsEnum.text.name(), "", getNoteText() );
        firePropertyChange( BindingsEnum.title.name(), "", getNoteTitle() );
    }

}
